package gui.data;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import util.SaveManager;
import util.savedResultWrapper;

public class SavedResultsGui extends JPanel implements ActionListener {

	JComboBox<String> selector;
	int selectedItemId = 0;
	LinkedList<savedResultWrapper> list;
	SaveManager manager;
	JButton open, delete;

	public SavedResultsGui() {
		manager = new SaveManager();
		this.setPreferredSize(new Dimension(500, 500));
		this.setLayout(new GridLayout(0, 2));
		selector = new JComboBox<String>();
		selector.addActionListener(this);
		open = new JButton("Open");
		open.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});
		delete = new JButton("Delete");
		delete.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				deleteResult();
			}
		});
		generateCombobox();
		this.add(selector);
		this.add(new JLabel());
		this.add(open);
		this.add(delete);
	}

	protected void deleteResult() {
		manager.removeSave(selectedItemId);
		generateCombobox();
	}

	protected void openResult() {
		savedResultWrapper savedResult = manager.getSave(selectedItemId);
		switch (savedResult.targetId) {
		// 2D 1-5
		// 3D 6-9
		// Others 10-11
		case 1:

			break;
		case 2:

			break;
		case 3:

			break;
		case 4:

			break;
		case 5:

			break;
		case 6:

			break;
		case 7:

			break;
		case 8:

			break;
		case 9:

			break;
		case 10:

			break;
		case 11:

			break;
		}
	}

	public void generateCombobox() {
		list = manager.getSaveList();
		Vector<String> entries = new Vector<String>();
		for (savedResultWrapper savedResult : list) {
			entries.add(savedResult.name);
		}
		DefaultComboBoxModel model = new DefaultComboBoxModel(entries);
		selector.setModel(model);
		if (entries.size() > 0) {
			selectedItemId = list.get(0).id;
			open.setEnabled(true);
			delete.setEnabled(true);
		} else {
			open.setEnabled(false);
			delete.setEnabled(false);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		selectedItemId = list.get(selector.getSelectedIndex()).id;
	}
}
